
Ext.define('Admin.EntitySimlet', {
    extend: 'Ext.ux.ajax.JsonSimlet',
    alias: 'simlet.entity',
    
    doPut:function(ctx){
        return this.doPost(ctx);
    },
    doPost: function(ctx) {
        var result = this.callParent(arguments),
            o = this.processData(Ext.decode(ctx.xhr.body)),
            item = this.getById(this.data, o.id, true),
            key;
        //for clone
        if(!item){
            item={id:this.data.length+1};
            this.data.push(item);
        }
            
        for (key in o) {
            if(key!='id')
                item[key] = o[key];
        }
        
        result.responseText = Ext.encode(item);
        return result;
    },
    
    processData: Ext.identityFn,

    getData: function (ctx) {
        var params = ctx.params;

        if ('id' in params) {
            return this.getById(this.data, params.id);
        }

        delete this.currentOrder;
        return this.callParent(arguments);
    },
    
    getById: function(data, id) {
        var len = data.length,
            i, item;
        
        for (i = 0; i < len; ++i) {
            item = data[i];
            if (item.id === id) {
                return item;
            }
        }
        return null;
    }
});

Ext.define('Admin.SimData', {
    requires: [
        'Ext.ux.ajax.*'
    ],

    singleton: true,

    dateFormat: 'Y-m-d\\TH:i:s\\Z',


    init: function () {
    	function uncapitalize(text) {
            if(!text || typeof text !== "string") {
               return ''; 
            }

            return text.charAt(0).toLowerCase() + text.substr(1);
        }
        function makeSim (data) {
            return {
                type: 'entity',
                data: data
            };
        }
     var me = this;
     var enm = Admin.Cfg.SIM_ENM;

     var sim_ens={};
  	for(var l in enm){
	var data = [];
	var en=l;
    var om = Ext.create("Admin.model."+en);
    for(var i=0; i<enm[l]; i++){
    	data.push(om.randData(i));
    }
    var url = '/rest/'+en;
    if(en == 'Attach') {
   	sim_ens[url] = Ext.apply({
        processFilters: function(filters) {
           Ext.each(filters, function (filter, index) {
                if (filter.property == 'articleId' ) {
                    filters[index] = function (r) {
                        return r.id < filter.value;
                    	
                    }
                }
            });
            return this.self.prototype.processFilters.call(this, filters);
        }
    }, makeSim(data));
   }else
    	sim_ens[url]=makeSim(data);
   }
  	var ulist=sim_ens['/rest/User'];
  	if(ulist){
  		var users = ulist.data;
  		sim_ens['/auth']= {
	        type: 'json',
	        data: function(ctx) {
                var userName = ctx.params.username,
                user = Ext.Array.findBy(users, function(item) {
                    return item.name === userName;
                }) || users[0];
                user.name=userName;
                return Ext.apply({}, user);
	        }
	    }  		
  	}
  sim_ens["/ls/net.bat.entity.User/name"]=sim_ens['/rest/User'];
  sim_ens["/distinct/net.bat.entity.User/loginName"]=sim_ens['/rest/User'];
  Admin.Cfg.SM =Ext.ux.ajax.SimManager.init().register(sim_ens);
  }
});